package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000119657944";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCU30IJFuaMbHOQc9ga8ZqaB/xnApim7027V40xyVnbWMrwNgZon0V/BdrKRasOQW2FBYG25quKttUz1n/WX5O/gAMMnGn2KiVpijtnMDx2/wSPXbPQLwvlRrkepd9uwdEKG4Ca5zkdoN9T1O7MLUe+wjUQLRe31zcDGiUIxM1nlw+gMY1V2Rxv9mtHXNrNPPCblx+GDjn/LpQoujkl7aqSrXkipq0NUcdlWxEQxpqxFJ/Hn8ccrrAc4nphNpOY5KivXeYXFWTFAM7qYX4Z8gxGGKiW+iljnNBmvKS/8GDkQBjKy+nxreWk76VGQf0gXK2Peu453ueRpmERYc9F64KHAgMBAAECggEAf1StcITL0FN6TJUow3LeXtxxv9Fyq2WGwLPTqj+jwX6ipO4wcQ+5Kzngtpz7/4dqmxnopn2JP2290UantL2u614cPY8S+l91/F0lDc8uCEF6rvzgLTHwJLxm3ZXWEy9nEhQP3W3jZjoQie1YCf64nKoTJLDM1osXHGoiPuvFG9G/K3pDWJOcmOSr7FGjCcFHvO38fSwaaLktAt5mH6+zsP9ZuXaR3bmKHi0YMO7JEpEiJ/FerTNWgqlBTfEIv3h/iYFHtD5krdFePkEWrwwbUp9QQ36BpfrWr+agEfasrqyzeiqswa9dZmf2ILs86MJnFS5rCgryxDYmK/e25aOXUQKBgQDqHLzQJaIzd+1VHyX/5HUmk+Sg2xuD+xeIGCE5T8UFJRGcWlBPV4P0Te44tjqBzhJNPuMaT2OJ0+hRy6/t3TrwZFT9fzLwvAfYHOeAJ46gT03OPGG6TNRwKQUh7W0zZSGnf/9VZI0mfAGM0mT9IjoyOtMmbe0KyiqUE9OoSi6wHQKBgQCiymAXoT51YmL2PPpc/XVdp1M9CyB+Jfqv5u5J4HQiARdxXMhYAlmc34KXdMaZzG3DTqyM8uqopW7folSSRnVSh9h3Fi4+Tr9PRtpTE1ck1XfuFGo/OW9hPhErxaqiROto8JAsLxn5MtyXnRjnF+MANvjVwrEpiVcpjethK5ID8wKBgQDcC3zQ676EP+G8U1WlKneOdg27v5YiPDzG918q/jZB+rCN1DmOCcUGOMWDWoyteyKqpkRRc3MslhfT4e6PVZRTZQqZZhhcO2/oCmrxgrzFIyUPhqOYtyQp8/RWClLMru6uAFnfSm59UKsoUI0JfEVU9wOGKGZX8OOKYQRdNrShZQKBgE4qNjGibscWUsZBSUXVPcGWUlWBPjzUJ6bGj6xRZfHd7E+2aUw49KZqe94VaDb4/ctFv3x+N0Z7CN3AqDMEOdlC+SFVOScZ0KpULwisxK8fANMEYm+xOFXM45nM872usgpVWCSfCS2qyljSSd7MjWbQ/nJFZaO4Gm4NgvPJY/cZAoGBAJecHzl+/Nl3aGI4JG2MXNw1eVecYojKCxyZSUblEtcOZ/sy73n7yc9Au+nXTHss3875mCwYhdENbMQZJLtTM/YUKWXB4SIFK967sxg7/Y5ysmLrNksCeOz3hpyShFFiv7KnA2tCZ++13rzKOkEVaCPla1TgLkQvgj5zfCPZ93Yj";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2X174pXP7Fm+dX8apP5aNccV8XVWisNtGQnEUzaG4rLM8FHlWP2kAg1u9BCBH3/TiQuhWNWbsGXlUSsaZUnxtvOmSUG9/KvpWLY73NcreBxywkHkmmb2fVTPWopHNFKslXsZvMafgKnaOxNBUaINstzh16YlNzH+cyJxu9OKTaN/1UQtrQm9rsbgQ0oNWci+zyCxCTiVEIs+m/CdLmsk4FlbwInXHjMMbXLA1tPbAG1QlZTwrncteckU6AtiOp8GaOrawGr7CJNgHxHgPYa8u9jGn+1IHCwuAf8N/9HMF5XD90GZkzFLzz8v5uviRfMWsI78AaF9TdhQ2oROGafOzwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://664d-183-6-127-212.ngrok.io/alipay_trade_page_pay_JAVA_UTF_8_war_exploded/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://664d-183-6-127-212.ngrok.io/alipay_trade_page_pay_JAVA_UTF_8_war_exploded/return_url.jsp";


    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
